package de.szut.listener;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import de.szut.dynamic.view.AbilityTreeTabContent;
import de.szut.entity.Ability;
import de.szut.entity.AbilityUpgrade;
import de.szut.entity.BaseAbility;
import de.szut.logic.Logic;
import de.szut.logic.ModifierStore;
import de.szut.splasher.R;
import de.szut.util.SkillAbilityStatus;

/**
 * Created by Dmitrij on 19.09.13.
 */
public class OnAbilityClickListener implements View.OnClickListener {

    private final int DESCRPTION_IMAGE_WIDTH = 32;

    private final int DESCRPTION_IMAGE_HEIGHT = 32;

    private final Context context;

    private Ability ability;

    private ViewGroup descriptionView;

    private TextView points;

    private TextView name;

    private ImageView image;

    private TextView text;

    private TextView contentDescription;

    // copy start here //
    private Button evolve;
    // copy end here //

    private ModifierStore modifierStore;

    public OnAbilityClickListener(Ability ability, ViewGroup descriptionView, Context ctx) {
        this.ability = ability;
        this.descriptionView = (ViewGroup) descriptionView.getChildAt(0);
        this.context = ctx;
        this.modifierStore = Logic.getInstance(context).getModifierStore();
    }

    /**
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        this.findViews();
        this.fillViews();
    }

    private void fillViews() {
        // copy start here (replace whole method)
        this.points.setText(String.valueOf(this.ability.getPoints()));
        this.contentDescription.setText(this.ability.getModifier().getType().getDescription());

        if(this.ability instanceof BaseAbility){
            this.name.setText(((BaseAbility) this.ability).getName());
            this.image.setImageDrawable(AbilityTreeTabContent.getDrawableByName(this.ability.getImage(), this.context));
            this.text.setText(((BaseAbility) this.ability).getDescription());
        } else {
            BaseAbility baseAbility = this.modifierStore.getBaseAbilityOf((AbilityUpgrade) this.ability);
            this.name.setText(baseAbility.getName() + ((AbilityUpgrade) this.ability).getRankNumber());
            this.image.setImageDrawable(AbilityTreeTabContent.getDrawableByName(baseAbility.getImage(), this.context));
            this.text.setText(baseAbility.getDescription());
        }

        this.setupEvolveButton();
        // copy end here
    }

    // copy whole method
    private void setupEvolveButton() {
        if(this.modifierStore.abilityIsSkillAble(this.ability)){
            this.evolve.setEnabled(true);
            this.evolve.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SkillAbilityStatus state = OnAbilityClickListener.this.modifierStore.buyAbility(ability);
                    if(state.equals(SkillAbilityStatus.SUCCESS)){
                        Toast.makeText(context, "Fähigkeit wurde erworben...", Toast.LENGTH_LONG).show();
                        // TODO: does toast work? use something else? seperate for every possible state?
                    } else {
                        Toast.makeText(context, "Diese Fähigkeit kann nicht erworben werden...", Toast.LENGTH_LONG).show();
                    }
                }
            });
            //TODO: maybe some style settings?
        } else {
            this.evolve.setEnabled(false);
            //TODO : also style settings? who knows...
        }
    }

    private void findViews() {
        this.points = (TextView) this.descriptionView.findViewById(R.id.ability_desc_points);
        this.name = (TextView) this.descriptionView.findViewById(R.id.ability_desc_name);
        this.image = (ImageView) this.descriptionView.findViewById(R.id.ability_desc_image);
        this.image.setLayoutParams((new TableRow.LayoutParams(DESCRPTION_IMAGE_WIDTH,DESCRPTION_IMAGE_HEIGHT)));
        this.text = (TextView) this.descriptionView.findViewById(R.id.ability_desc_text);
        this.contentDescription = (TextView) this.descriptionView.findViewById(R.id.ability_content_desc_text);
        this.descriptionView.setVisibility(View.VISIBLE);
        // copy start here //
        this.evolve = (Button) this.descriptionView.findViewById(R.id.evolve_ability_btn);
        // copy end here
    }
}
